<include file="Public/header"/>
<style>
    .park-form__radioInput.cur{background:url(__ROOT__/Public/Home/images/radio_press.png) no-repeat;background-size:20px;}
</style>
<body class="body-bg">
<div class="park-header">
    <div class="park-header__title">购物车</div>
    <div class="park-header__right" onclick="edit_cart(this);">编辑</div>
</div>
<div class="container mb-cartFooter">
    <div class="park-cart-store-list">
        <volist name="store" id="val">
        <div class="park-cart-store-item">
            <div class="park-cart-store-item__title">
                <div class="left">
                    <label class="park-form">
                        <i class="park-form__radioInput park-form__radio" onclick="store_check(this);"></i>
                    </label>
                    &nbsp;&nbsp;
                    <i class="iconfont icon-dian"></i>
                    &nbsp;&nbsp;
                    <span>{$val.store_name}</span>
                </div>
            </div>
            <div class="park__goods">
                <ul>
                    <volist name="val['cart_list']" id="vo">
                    <li>
                        <div class="park-goods__radio">
                            <label class="park-form">
                                <i class="park-form__radioInput park-form__radio radio{$vo.id}" data-id="{$vo.id}" data-price="{$vo.spec_price}" data-number="{$vo.goods_number}"></i>
                            </label>
                        </div>
                        <div class="park-goods__pic" style="background-image: url('{$vo.thumb}')"></div>
                        <div class="park-goods__info">
                            <p class="name">{$vo.goods_name}</p>
                            <p class="desc">{$vo.keywords}</p>
                            <p class="spec">规格：{$vo.spec_name}</p>
                            <p class="price-line">
                                <span class="price">￥<em>{:intval($vo['spec_price'])}</em>{:substr($vo['spec_price'],-3,3)}</span>
                            </p>
                        </div>

                        <div class="park-goods__pos">
                            <div class="park-goods-count">
                                <i class="iconfont icon-jian" onclick="add_cart(this,1,{$vo.goods_id},{$val.store_id},{$vo.spec_id},{$vo.id});"></i>
                                <span>{$vo.goods_number}</span>
                                <i class="iconfont icon-jia" onclick="add_cart(this,2,{$vo.goods_id},{$val.store_id},{$vo.spec_id},{$vo.id});"></i>
                            </div>
                        </div>
                    </li>
                    </volist>
                </ul>
            </div>
        </div>
        </volist>
    </div>
</div>
<div class="park-cart-total">
    <div class="park-cart-total__input">
        <label class="park-form">
            <span class="park-form__radioInput park-form__radio" onclick="all_check(this)"></span>
            <span class="park-form__text">全选</span>
        </label>
    </div>
    <div class="park-cart-total__price" id="edit_cart01">
        <p class="total" id="all_price" data="0">合计：<span class="price">￥<span>0</span>.00</span></p>
        <p class="discount">已省：￥0（不含运费）</p>
    </div>
    <div class="park-cart-total__submit" onclick="confirm_order();" id="edit_cart02">
        去结算
    </div>
    <div class="park-cart-total__price" id="edit_cart04" style="display:none;"></div>
    <div class="park-cart-total__submit" onclick="del_order();" id="edit_cart03" style="display:none;background:red;">
        删除
    </div>
</div>
<include file="Public/footer"/>
</body>
<script src="__ROOT__/Public/Home/layer/layer.js"></script>
<script>
    $('li .park-form__radio').click(function(){
        if($(this).hasClass('cur')){
            $(this).removeClass('cur');
        }else {
            $(this).addClass('cur');
        }
        count_price();
    });
    function count_price(){
        window.all_price = 0;
        $('li .park-form__radio.cur').each(function(){
            window.all_price = accSub($(this).attr('data-price')*$(this).attr('data-number'),all_price);
        });
        if(all_price>0) {
            $('#all_price').html('合计：<span class="price">￥<span>' + parseInt(all_price) + '</span>' + all_price.slice(-3) + '</span>');
        }else{
            $('#all_price').html('合计：<span class="price">￥<span>0</span>.00</span>');
        }
    }
    function accSub(arg1,arg2,type){
        var r1,r2,m,n;
        try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
        try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
        m=Math.pow(10,Math.max(r1,r2));
        //last modify by deeka
        //动态控制精度长度
        n=(r1>=r2)?r1:r2;
        if(type==1){
            return ((arg1*m-arg2*m)/m).toFixed(2);
        }else{
            return ((arg1*m+arg2*m)/m).toFixed(2);
        }
    }
    function add_cart(obj,num,goods_id,store_id,spec_id,id){
        if(num==1){
            var cart = parseInt($(obj).next().html());
            if(cart==1){
                layer.open({
                    content: '确定要删除该商品吗？'
                    ,btn: ['确定', '取消']
                    ,yes: function(index){
                        $.post('/Home/Cart/del_cart/',{goods_id:goods_id,store_id:store_id,spec_id:spec_id},function(data){
                            window.location = '/Home/Cart/index';
                        },'json');
                    }
                });
                return false;
            }
            cart=cart-1;
            $(obj).next().html(cart);
        }else{
            var cart = parseInt($(obj).prev().html());
            cart=cart+1;
            $(obj).prev().html(cart);
        }
        $.post('/Home/Cart/add_cart/',{goods_id:goods_id,store_id:store_id,spec_id:spec_id,goods_number:1,type:num},function(){
            $('.radio'+id).attr('data-number',cart);
            count_price();
        },'json');
    }
    function all_check(obj){
        var check = $(obj).attr('data');
        if(check){
            $(obj).attr('data','');
            $('.park-form__radio').each(function () {
                $(this).removeClass('cur');
            });
        }else {
            $(obj).attr('data',true);
            $('.park-form__radio').each(function () {
                $(this).addClass('cur');
            });
        }
        count_price();
    }
    function store_check(obj){
        if($(obj).hasClass('cur')){
            $(obj).removeClass('cur').parent().parent().parent().next().find('.park-form__radio').removeClass('cur');
        }else {
            $(obj).addClass('cur').parent().parent().parent().next().find('.park-form__radio').addClass('cur');
        }
        count_price();
    }
    //去结算
    function confirm_order(){
        window.all_id = '';
        $('li .park-form__radio.cur').each(function () {
            window.all_id+=$(this).attr('data-id')+',';
        });
        if(!all_id){
            layer.open({
                content:'至少要购买一件'
                ,skin: 'msg'
                ,time: 2 //2秒后自动关闭
            });
            return false;
        }
        window.location = '/Home/Cart/confirm_order/cart_id/'+all_id;
    }
    //编辑购物车
    function edit_cart(obj){
        if($(obj).html()=='编辑'){
            $('#edit_cart01').hide();
            $('#edit_cart02').hide();
            $('#edit_cart03').show();
            $('#edit_cart04').show();
            $(obj).html('完成');
        }else{
            $('#edit_cart03').hide();
            $('#edit_cart04').hide();
            $('#edit_cart01').show();
            $('#edit_cart02').show();
            $(obj).html('编辑');
        }
    }
    //删除购物车
    function del_order(){
        layer.open({
            content: '确定要删除吗？'
            ,btn: ['确定', '取消']
            ,yes: function(index){
                window.all_id = '';
                $('li .park-form__radio.cur').each(function () {
                    window.all_id+=$(this).attr('data-id')+',';
                });
                if(!all_id){
                    layer.open({
                        content:'至少要选择一件'
                        ,skin: 'msg'
                        ,time: 2 //2秒后自动关闭
                    });
                    return false;
                }
                $.post('/Home/Cart/del_all_cart/',{cart_id:all_id},function(data){
                    window.location = '/Home/Cart/index/';
                },'json');
            }
        })

    }
</script>
</html>